package com.vf.admin.miaodong.controller;

import com.aliyun.oss.OSSClient;
import com.vf.admin.client.service.AppPrivilegeService;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppHeadwear;
import com.vf.admin.miaodong.entity.AppMounts;
import com.vf.admin.miaodong.entity.AppPrivilege;
import com.vf.admin.miaodong.service.AfterGiftService;
import com.vf.admin.miaodong.service.AfterHeadwearService;
import com.vf.admin.miaodong.service.AfterMountsService;
import com.vf.admin.miaodong.service.AfterPrivilegeService;
import com.vf.admin.utils.Base64Image.AliyunOSSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.util.*;

/**
 * 贵族等级管理
 */
@Controller
@RequestMapping("/after/privilege")
public class AfterPrivilegeController extends BaseController {

    @Autowired
    AfterPrivilegeService afterPrivilegeService;
    @Autowired
    AfterHeadwearService afterHeadwearService;
    @Autowired
    AfterMountsService afterMountsService;
    @Autowired
    AfterGiftService afterGiftService;
    @Autowired
    AppPrivilegeService appPrivilegeService;


    /**
     * 获取贵族列表
     * @param splitPage
     * @param name  真实姓名
     * @return
     */
    @RequestMapping("/list")
    public String list(SplitPageBean splitPage, String phone, String name, Integer status){
        try{
            List<AppPrivilege> all = afterPrivilegeService.list(null,splitPage);
            request.setAttribute("list",all);
            request.setAttribute("splitPage",splitPage);
            return this.sucess("/web/after/grade/guiZuList");
        }catch (Exception e){
            return this.error("系统繁忙");
        }
    }

    /**
     * 贵族等级详情保存
     *
     * @return
     */
    @RequestMapping("/guiZuSave")
    @ResponseBody
    public Result guiZuSave(AppPrivilege appGifts,String medals,String frames,String roomnoblepics, HttpServletRequest request) {
        try {
            AppPrivilege appPrivilege = new AppPrivilege();
            appPrivilege.setName(appGifts.getName());
            appPrivilege.setIstoushi(appGifts.getIstoushi());
            if(appGifts.getIstoushi()==1){
                AppHeadwear headwear=afterHeadwearService.get(appGifts.getToushiid());
                appPrivilege.setToushiid(headwear.getId());
                appPrivilege.setToushinick(headwear.getName());
                appPrivilege.setToushipic(headwear.getImgurl());
            }
            appPrivilege.setIszuoqi(appGifts.getIszuoqi());
            if(appGifts.getIszuoqi()==1){
                System.out.println("坐骑id:"+appGifts.getZuoqiid());
                AppMounts mounts=afterMountsService.get(appGifts.getZuoqiid());
                appPrivilege.setZuoqiid(mounts.getId());
                appPrivilege.setZuoqinick(mounts.getName());
                appPrivilege.setZuoqipic(mounts.getImgurl());
            }
            appPrivilege.setId(appGifts.getId());
            appPrivilege.setMoney(appGifts.getMoney());
            appPrivilege.setFreetime(appGifts.getFreetime());
            appPrivilege.setMedal(medals);
            appPrivilege.setFrame(frames);
            appPrivilege.setXuanchuan(roomnoblepics);
            afterPrivilegeService.save(appPrivilege);
            return sucess();
        } catch (Exception e) {
            return error();
        }
    }


    /**
     * 获取贵族详情
     */
    @RequestMapping(value = "/edit")
    public String edit(Integer id) {
        try {
            AppPrivilege byId = afterPrivilegeService.get(id);
            Map<String,Object> toushiMap=new HashMap<>(16);
            toushiMap.put("guishu",1);
            List<AppHeadwear> toushiList=afterHeadwearService.findList(toushiMap,null);
            List<AppMounts> zuoqiList=afterMountsService.findList(toushiMap,null);

            List<AppPrivilege> guizuList = appPrivilegeService.guizuList();
            for (AppPrivilege appPrivilege : guizuList) {
                Constants.guizuCacheMap.put(appPrivilege.getId(), appPrivilege.getMedal());
                Constants.guizuMingMap.put(appPrivilege.getId(), appPrivilege.getName());
            }
            request.setAttribute("byId", byId);
            request.setAttribute("toushiList", toushiList);
            request.setAttribute("zuoqiList", zuoqiList);
            return sucess("/web/after/grade/guiZuEdit");
        } catch (Exception e) {
            return this.error("系统异常");
        }
    }

    @RequestMapping("/upload")
    @ResponseBody
    public Result warningList(MultipartFile file, HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<>();
        String resultStr = null;
        try {
            String name = file.getOriginalFilename();
            String suffix = name.substring(name.indexOf("."));
            String randomName = UUID.randomUUID().toString().replaceAll("-", "");
            String fileName = randomName + suffix;
            Long size = file.getSize();
            // 创建OSSClient实例
            OSSClient ossClient = new OSSClient(AliyunOSSClientUtil.ENDPOINT, AliyunOSSClientUtil.ACCESSKEYID, AliyunOSSClientUtil.ACCESSKEYSECRET);
            // 上传：picturePath + uploadFile.getOriginalFilename() --> key
            ossClient.putObject(AliyunOSSClientUtil.BUCKETNAME, AliyunOSSClientUtil.KEY + fileName, new ByteArrayInputStream(file.getBytes()));
            // 关闭client
            ossClient.shutdown();
            //设置过期时间 -- 十年
            Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
            //取出文件上传路径
            String url = ossClient.generatePresignedUrl(AliyunOSSClientUtil.BUCKETNAME, AliyunOSSClientUtil.KEY + fileName, expiration).toString();
            String aaa = url.substring(0, url.indexOf("?"));
            Map<String, String> mm = new HashMap<>();
            // AppSetup byId = appSetupService.getById(1);
            //String pictureprefix = byId.getPictureprefix();
            //mm.put("pictureprefix",pictureprefix);
            mm.put("name", aaa);
            return sucess(mm);
        } catch (Exception e) {
          /*  map.put("code",1);
            map.put("msg","上传失败");*/
            return errorJSON("上传失败");
        }
    }








}
